using System;
using System.Collections.Generic;
using UnityEngine;

public class MainThreadDispatcher : UnitySingleton<MainThreadDispatcher>
{
    private readonly Queue<Action> _actionQueue = new Queue<Action>();
    private object _queueLock = new object();

    private void Update()
    {
        lock (_queueLock)
        {
            while (_actionQueue.Count > 0)
            {
                Action action = _actionQueue.Dequeue();
                action?.Invoke();
            }
        }
    }

    public void Enqueue(Action action)
    {
        lock (_queueLock)
        {
            _actionQueue.Enqueue(action);
        }
    }
}